package org.jeecg.modules.clues.jobs;


import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;

import org.jeecg.modules.clues.entity.Clues;
import org.jeecg.modules.clues.service.ICluesService;

import org.jeecgframework.p3.core.util.HttpUtils;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 中国日报线索爬虫
 */
@Slf4j
public class ZGRBCluesJob implements Job {

    /**
     * 若参数变量名修改 QuartzJobController中也需对应修改
     */
    private String parameter;

    public void setParameter(String parameter) {
        this.parameter = parameter;
    }

    @Autowired
    private ICluesService cluesService;


    @SneakyThrows
    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        List<String> keywords = cluesService.getAllKeyword();
        if(!CollectionUtils.isEmpty(keywords)){
            List<Clues> crawlerInfoList = getZGRBPageResponse(keywords, Integer.valueOf(parameter));
            cluesService.saveOrUpdateBatch(crawlerInfoList);
        }

    }


    /**
     * https://so.toutiao.com/search?keyword=%E5%8D%97%E4%BA%AC&dvpf=pc&page_num=1&min_time=1628006400&max_time=1628092799
     * 中国日报
     * @param
     * @return
     * @throws Exception
     */
    public static List<Clues> getZGRBPageResponse(List<String > keywords,Integer page) throws Exception {
        List<Clues>  clueList = new ArrayList<>();
        String  zgrbUrl = "https://newssearch.chinadaily.com.cn/rest/cn/search?sort=dp&curType=story";
        if(!CollectionUtils.isEmpty(keywords)){
            for(int i =0;i<=page;i++){
                for (String keyword : keywords) {
                    String  url = zgrbUrl+"&keywords="+keyword+"&page="+i;
                    Thread.sleep(3000);
                    String s = HttpUtils.get(url);
                    JSONObject jsonObject = JSONObject.parseObject(s);
                    if(jsonObject !=null){
                        JSONArray jsonArray = jsonObject.getJSONArray("content");
                        if(jsonArray !=null){
                            for (Object o : jsonArray) {
                                Clues clue = new Clues();
                                JSONObject content = JSONObject.parseObject(o.toString());
                                String id=  content.get("id").toString();
                                String articleUrl=  content.get("url").toString();
                                String title =  content.get("title").toString();
                                clue.setId("zgrb"+id);
                                clue.setKeyword(keyword);
                                clue.setCluesTitle(title);
                                clue.setCrawlerUrl(articleUrl);
                                clue.setCluesSource(1);
                                clue.setCrawlerSource(2);
                                clueList.add(clue);
                            }
                        }
                    }
                }
            }
        }
        return   clueList ;
    }

}
